package com.xinnet.mms.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xinnet.mms.contact.service.ContactService;
import com.xinnet.mms.entity.Contact;
import com.xinnet.mms.entity.Customer;
import com.xinnet.mms.member.service.MemberService;

@Controller
@RequestMapping(value = "/contact")
public class ContactController {
	
	@Autowired
	ContactService contactService;
	
	@Autowired
	MemberService memberService;
	
	ObjectMapper mapper = new ObjectMapper();
	/**
	 * 联系人页面
	 * @param request	 * @param response
	 * @return 
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/addContact")
	public void addContact(Contact contact, HttpServletRequest request, HttpServletResponse response,
			HttpSession session, Model model){
		PrintWriter pw=null;
		try {
			
			pw = response.getWriter();
			/*List<Contact> contactList = contactService.queryContact(contact);
			if(contactList!=null&&!contactList.isEmpty()){
				pw.print("同一个会员只能有一条联系人！");
				return;
			}*/
//			contact.setId(UUID.randomUUID().toString());
			//先查询一下
			Contact contactQuery = contactService.queryContact(contact);
			boolean isOk = false;
			if(contactQuery==null){
				isOk = contactService.addContact(contact);
			} else {
				//更新
				isOk = contactService.updateContact(contact);
			}
			if(isOk){
				pw.print("修改成功！");
			} else {
				pw.print("修改失败！");
			}
			
		} catch (Exception e) {
			pw.print("修改失败:"+e.getMessage());
		}
	}
	
	/**
	 * 联系人页面
	 * @param request	 * @param response
	 * @return 
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/queryContact")
	public void queryContact(Contact contact, HttpServletRequest request, HttpServletResponse response,
			HttpSession session, Model model){
		PrintWriter pw=null;
		try {
			pw = response.getWriter();
			contact = contactService.queryContact(contact);
			String customerListStr = mapper.writeValueAsString(contact);
			pw.print(customerListStr);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 获取并同步联系人信息
	 * @param contact
	 */
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false)
	@RequestMapping(value = "/sysnContact")
	public void sysnContact(HttpServletRequest request, HttpServletResponse response,Contact contact,Model model
			){
		String cusId = contact.getCusId();
		Customer customer= null;
		PrintWriter pw=null;
		try {
			pw = response.getWriter();
			customer = memberService.getMemberByCusId(cusId);
			BeanUtils.copyProperties(customer, contact);
			
			contact.setMobile(customer.getPhone());
			contact.setCname(customer.getCustomName());
			contact.setUname(customer.getOrganizeNameCN());
			Contact contactQuery = contactService.queryContact(contact);
			if(contactQuery==null){
				contactService.addContact(contact);
			} else {
				//更新
				contactService.updateContact(contact);
			}
			pw.print("同步信息");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
	}
	
}
